package com.wxzz.elearing.comment.service.impl;

import com.github.pagehelper.PageInfo;
import com.wxzz.elearing.comment.dto.ImproveCommentDto;
import com.wxzz.elearing.comment.entity.ImproveComment;
import com.wxzz.elearing.comment.service.IImproveCommentService;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ImproveCommentServiceImpl implements IImproveCommentService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public ReturnVO saveComment(ImproveComment comment) {
//        Skill skillInfo = skillMapper.selectById(comment.getSkillId());
//        if (skillInfo==null)
//            throw new ServiceException("请选择正确的技能");

        comment.setCreateTime(LocalDateTime.now());
        ImproveComment insert = mongoTemplate.insert(comment);
        // 增加 评论数量
//        Skill saveSkill = new Skill();
//        saveSkill.setId( skillInfo.getId() );
//        saveSkill.setCommentNum( skillInfo.getCommentNum()+1 );
//        skillMapper.updateById( saveSkill );

        return new ReturnVO("评论成功", insert.getUid());
    }

    @Override
    public ReturnVO<JgPageVo<List<ImproveComment>>> listComment(ImproveCommentDto improveCommentDto) {
        List<ImproveComment> list = new ArrayList();
        Query query1 = new Query();
        String ImproveId = String.valueOf(improveCommentDto.getImproveId());
        ImproveComment comment = mongoTemplate.findById(ImproveId, ImproveComment.class);
        query1.with(new Sort(Sort.Direction.DESC, "createTime")).with(new PageRequest(improveCommentDto.getCurPage() - 1, improveCommentDto.getMaxPage()));
        query1.addCriteria(Criteria.where("enterpriseImproveId").is(improveCommentDto.getImproveId())).addCriteria(Criteria.where("_id").ne(ImproveId));
        Query queryCount = new Query();
        queryCount.addCriteria(Criteria.where("enterpriseImproveId").is(improveCommentDto.getImproveId()));
        long count = mongoTemplate.count(queryCount, "improveCommentCollection");
        list = mongoTemplate.find(query1, ImproveComment.class);
//        if (improveCommentDto.getCurPage() == 1) {
//            list.add(0, comment);
//            if (list.size() != 1 && list.size() > 9) {
//                list.remove(list.size() - 1);
//            }
//        }
        JgPageVo jgPageVo = new JgPageVo<>(new PageInfo(list));
        jgPageVo.setCurIndex(improveCommentDto.getCurPage());
        jgPageVo.setPageSize(improveCommentDto.getMaxPage());
        jgPageVo.setPagesCount((int) Math.ceil(count * 1.0 / improveCommentDto.getMaxPage()));
        jgPageVo.setData(list);
        jgPageVo.setCount(count);
        return new ReturnVO("评论查询成功", jgPageVo);
    }

    @Override
    public ReturnVO deleteComment(String uid) {
        Query query = Query.query(Criteria.where("uid").is(uid));
        mongoTemplate.findAllAndRemove(query, ImproveComment.class);
        return new ReturnVO("改善评论删除成功", null);
    }
}
